@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM ========================================
REM 构建并推送 Docker 镜像到阿里云 ACR
REM ========================================

REM 配置参数
set "ACR_REPO=crpi-j39i6g2qk4ln00fg.cn-chengdu.personal.cr.aliyuncs.com/testnamespace-zb"
set "IMAGE_NAME=mskml-web"
set "TAG=%1"
if "%TAG%"=="" set "TAG=latest"

REM 获取脚本所在目录（项目根目录）
set "SCRIPT_DIR=%~dp0"
cd /d "%SCRIPT_DIR%"

echo ========================================
echo Build and Push Docker Image
echo ========================================
echo.

REM ========================================
REM 步骤 1: 构建 Docker 镜像
REM ========================================
echo [1/4] Building Docker image...
echo Image name: %IMAGE_NAME%:%TAG%
echo Build directory: %SCRIPT_DIR%
echo.

docker build -t %IMAGE_NAME%:%TAG% .

if errorlevel 1 (
    echo [ERROR] Docker build failed!
    exit /b 1
)

REM 验证镜像是否存在
timeout /t 1 /nobreak >nul
docker images %IMAGE_NAME% --format "{{.Repository}}:{{.Tag}}" | findstr /C:"%IMAGE_NAME%:%TAG%" >nul
if errorlevel 1 (
    echo [ERROR] Image not found after build: %IMAGE_NAME%:%TAG%
    echo Current images:
    docker images | findstr %IMAGE_NAME%
    exit /b 1
)

echo [OK] Docker image build completed: %IMAGE_NAME%:%TAG%
echo.

REM ========================================
REM 步骤 2: 打标签
REM ========================================
echo [2/4] Tagging image...

set "LOCAL_IMAGE=%IMAGE_NAME%:%TAG%"
set "REMOTE_IMAGE=%ACR_REPO%/%IMAGE_NAME%:%TAG%"

echo Local image: !LOCAL_IMAGE!
echo Remote image: !REMOTE_IMAGE!
echo.

docker tag !LOCAL_IMAGE! !REMOTE_IMAGE!

if errorlevel 1 (
    echo [ERROR] Docker tag failed!
    echo Please check image name format
    exit /b 1
)

echo [OK] Image tagged: !REMOTE_IMAGE!
echo.

REM ========================================
REM 步骤 3: 推送到阿里云 ACR
REM ========================================
echo [3/4] Pushing to Alibaba Cloud ACR...
echo Remote image: !REMOTE_IMAGE!
echo.
echo Pushing layers (this may take a while)...
echo.

docker push !REMOTE_IMAGE!

if errorlevel 1 (
    echo.
    echo [ERROR] Docker push failed!
    echo Please login: docker login %ACR_REPO%
    exit /b 1
)

echo.
echo [OK] Image pushed successfully!

echo.
echo ========================================
echo [SUCCESS] Build and push completed!
echo Image: !REMOTE_IMAGE!
echo ========================================
echo.

REM ========================================
REM 步骤 4: 清理本地镜像（可选）
REM ========================================
echo [4/4] Cleaning up local images...
echo.

REM 删除远程标签的镜像
docker rmi !REMOTE_IMAGE! 2>nul
if errorlevel 0 (
    echo [OK] Removed remote tagged image: !REMOTE_IMAGE!
) else (
    echo [INFO] Remote tagged image may already be deleted
)

REM 删除本地镜像（可选，注释掉如果不想删除）
REM docker rmi !LOCAL_IMAGE! 2>nul
REM if errorlevel 0 (
REM     echo [OK] Removed local image: !LOCAL_IMAGE!
REM )

echo [OK] Cleanup completed!
echo.

echo ========================================
echo All operations completed successfully!
echo ========================================
echo.

pause

endlocal

